<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="组织名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入组织名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="城市级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择城市级别" clearable>
          <el-option v-for="dict in dict.type.CityLevel" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="sysOrganizationList" row-key="id"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column label="组织名称" align="left" prop="name" width="150"/>
      <el-table-column label="编号" align="left" prop="code" width="120" />
      <el-table-column label="类型" align="left" prop="type" width="80">
        <template #default="scope">
          <dict-tag :options="dict.type.OrgType" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="经纬度" align="left" prop="latitude" width="180"/>
      <el-table-column label="城市级别" align="left" prop="level" width="80">
        <template #default="scope">
          <dict-tag :options="dict.type.CityLevel" :value="scope.row.level" />
        </template>
      </el-table-column>
      <el-table-column label="地址" align="left" prop="address" width="250" />
      <el-table-column label="超期承运商" align="left" prop="isOverdueCarrier">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.isOverdueCarrier" />
        </template>
      </el-table-column>
      <el-table-column label="使用超期合同" align="left" prop="isOverdueContract">
        <template #default="scope">
          <dict-tag :options="dict.type.CommIsNot" :value="scope.row.isOverdueContract" />
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="left" prop="principal" />
      <el-table-column label="状态" align="left" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.CommDataStatus" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['kysys:sysOrganization:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['kysys:sysOrganization:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改网点机构对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="父级" prop="zid">
          <treeselect v-model="form.zid" :options="sysOrganizationOptions" :normalizer="normalizer" placeholder="请选择父级" disabled="disabled"/>
        </el-form-item>
        <el-form-item label="组织名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入组织名称" readonly="readonly"/>
        </el-form-item>
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" readonly="readonly"/>
        </el-form-item>
        <el-form-item label="组织机构类型" prop="type">
          <el-select clearable v-model="form.type" placeholder="请选择组织机构类型">
            <el-option v-for="dict in dict.type.OrgType" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
        <el-form-item label="经纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入经纬度" />
        </el-form-item>
        <el-form-item label="城市级别" prop="level">
          <el-select clearable v-model="form.level" placeholder="请选择城市级别">
            <el-option v-for="dict in dict.type.CityLevel" :key="dict.value" :label="dict.label"
              :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="使用超期承运商" prop="isOverdueCarrier">
          <el-radio-group v-model="form.isOverdueCarrier">
            <el-radio v-for="dict in dict.type.CommIsNot" :key="dict.value"
              :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="使用超期合同" prop="isOverdueContract">
          <el-radio-group v-model="form.isOverdueContract">
            <el-radio v-for="dict in dict.type.CommIsNot" :key="dict.value"
              :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="生效状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.CommDataStatus" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysOrganization, getSysOrganization, delSysOrganization, addSysOrganization, updateSysOrganization } from "@/api/kysys/sysOrganization";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SysOrganization",
  dicts: ['CityLevel', 'CommIsNot', 'CommDataStatus', 'OrgType'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 网点机构表格数据
      sysOrganizationList: [],
      // 网点机构树选项
      sysOrganizationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 预录单序列时间范围
      daterangeCreateTime: [],
      // 预录单序列时间范围
      daterangeModifyTime: [],
      // 查询参数
      queryParams: {
        deptId: null,
        code: null,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        latitude: null,
        level: null,
        type: null,
        zid: null,
        address: null,
        isOverdueCarrier: null,
        isOverdueContract: null,
        principal: null,
        orderCode: null,
        dispatchCode: null,
        shipmentCode: null,
        vehiclePlanCode: null,
        prescoCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入代码", trigger: "blur" }],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
        principal: [{ required: true, message: "请输入负责人", trigger: "blur" }],
        level: [{ required: true, message: "请选择城市级别", trigger: "blur" }],
        type: [{ required: true, message: "请选择组织机构类型", trigger: "blur" }],
        isOverdueCarrier: [{ required: true, message: "请选择是否使用超期承运商", trigger: "blur" }],
        isOverdueContract: [{ required: true, message: "请选择是否使用超期合同", trigger: "blur" }],
        status: [{ required: true, message: "请选择生效状态", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网点机构列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' !== this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      listSysOrganization(this.queryParams).then(response => {
        if (response.data) {
          var data = response.data.filter(item => item.code != 'root');
          this.sysOrganizationList = this.handleTree(data, "id", "zid");
        }
        this.loading = false;
      });
    },
    /** 转换网点机构数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    /** 查询网点机构下拉树结构 */
    getTreeselect() {
      listSysOrganization().then(response => {
        this.sysOrganizationOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "zid");
        this.sysOrganizationOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        code: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        latitude: null,
        level: null,
        type: null,
        zid: null,
        address: null,
        isOverdueCarrier: null,
        isOverdueContract: null,
        principal: null,
        orderCode: null,
        dispatchCode: null,
        shipmentCode: null,
        vehiclePlanCode: null,
        prescoCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.zid = row.id;
      } else {
        this.form.zid = 0;
      }
      this.open = true;
      this.title = "添加网点机构";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.zid = row.id;
      }
      getSysOrganization(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网点机构";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysOrganization(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysOrganization(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除网点机构编号为"' + row.id + '"的数据项？').then(function () {
        return delSysOrganization(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
